import 'dart:async';

typedef ThrottleCallback = void Function();

/// 节流
class UtilsThrottler {
  final int delay;
  Timer? _timer;
  bool _isThrottling = false;

  UtilsThrottler({this.delay = 500});

  void throttle(ThrottleCallback callback) {
    if (_isThrottling) return;

    _isThrottling = true;
    callback();

    _timer = Timer(Duration(milliseconds: delay), () {
      _isThrottling = false;
      _timer?.cancel();
    });
  }
}